﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace _1065093_1065130_1065120_DoAn
{
    public partial class QuanLySanPham : UserControl
    {
        public QuanLySanPham()
        {
            InitializeComponent();
        }

        public void LoadSP()
        {
            SANPHAMBUS spBUS = new SANPHAMBUS();
            DataTable dt = new DataTable();
            dt = spBUS.LoadDsSanPham();
            dgvdanhsachsp.DataSource = dt;
        }

        public void clr()
        {
            txttensp.Text = "";
            txtgioihankho.Text = "";
            txtgioihanquay.Text = "";
            txtgia.Text = "";
            txtslhttrongkho.Text = "";
            txtslhttrongquay.Text = "";
            lbmasp.Text = "";
        }

        public void LoadTim()
        {
            string tukhoa = txtsearch.Text;
            SANPHAMBUS spBus = new SANPHAMBUS();
            DataTable dt = new DataTable();
            dt = spBus.LoadDsTimKiem(tukhoa);
            dgvdanhsachtimkiem.DataSource = dt;
        }

        private void QuanLySanPham_Load(object sender, EventArgs e)
        {
            LOAISPBUS loaispBUS = new LOAISPBUS();
            DataTable dtLoaiSP = new DataTable();
            dtLoaiSP = loaispBUS.LoadDsLoaiSP();
            cmbloaisp.DataSource = dtLoaiSP;
            cmbloaisp.DisplayMember = "MaLoaiSP";
            cmbloaisp.ValueMember = "MaLoaiSP";

            XUATXUBUS xxBUS = new XUATXUBUS();
            DataTable dtXX = new DataTable();
            dtXX = xxBUS.LoadDsXuatXu();
            cmbmaxx.DataSource = dtXX;
            cmbmaxx.DisplayMember = "MaXX";
            cmbmaxx.ValueMember = "MaXX";

            KHOBUS khoBUS = new KHOBUS();
            DataTable dtKho = new DataTable();
            dtKho = khoBUS.LoadDsKho();
            cmbmakho.DataSource = dtKho;
            cmbmakho.DisplayMember = "MaKho";
            cmbmakho.ValueMember = "MaKho";

            QUAYBUS quayBUS = new QUAYBUS();
            DataTable dtQuay = new DataTable();
            dtQuay = quayBUS.LoadDsQuay();
            cmbmaquay.DataSource = dtQuay;
            cmbmaquay.DisplayMember = "MaQuay";
            cmbmaquay.ValueMember = "MaQuay";

            cmbtinhtrang.Items.Add("CH");
            cmbtinhtrang.Items.Add("HH");
            cmbtinhtrang.Text = "CH";

            LoadSP();
            clr();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            try
            {
                string tensp = txttensp.Text;
                int maxx = Convert.ToInt32(cmbmaxx.SelectedValue.ToString());
                int ghkho = Convert.ToInt32(txtgioihankho.Text);
                int ghquay = Convert.ToInt32(txtgioihanquay.Text);
                int slhttrongkho = Convert.ToInt32(txtslhttrongkho.Text);
                int slhttrongquay = Convert.ToInt32(txtslhttrongquay.Text);
                int loaisp = Convert.ToInt32(cmbloaisp.SelectedValue.ToString());
                int makho = Convert.ToInt32(cmbmakho.SelectedValue.ToString());
                int maquay = Convert.ToInt32(cmbmaquay.SelectedValue.ToString());
                int gia = Convert.ToInt32(txtgia.Text);
                string tinhtrang = cmbtinhtrang.Text;
                DateTime ngaynhap = Convert.ToDateTime(dtpngaynhap.Text);

                SANPHAMBUS spBus = new SANPHAMBUS();
                SANPHAMDTO spDto = new SANPHAMDTO(-1,tensp,maxx,ghkho,ghquay,slhttrongkho,slhttrongquay,loaisp,makho,maquay,gia,tinhtrang,ngaynhap);
                spBus.ThemSP(spDto);
                LoadSP();
                clr();
                MessageBox.Show("Thành công");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có muốn xoá?","", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                try
                {
                    int ma = Convert.ToInt32(dgvdanhsachsp.CurrentRow.Cells[0].Value);

                    SANPHAMBUS spBus = new SANPHAMBUS();
                    spBus.XoaSP(ma);
                    LoadSP();
                    clr();
                    MessageBox.Show("Thành công");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn có muốn sửa?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                try
                {
                    int ma = Convert.ToInt32(lbmasp.Text);
                    string tensp = txttensp.Text;
                    int maxx = Convert.ToInt32(cmbmaxx.SelectedValue.ToString());
                    int ghkho = Convert.ToInt32(txtgioihankho.Text);
                    int ghquay = Convert.ToInt32(txtgioihanquay.Text);
                    int slhttrongkho = Convert.ToInt32(txtslhttrongkho.Text);
                    int slhttrongquay = Convert.ToInt32(txtslhttrongquay.Text);
                    int loaisp = Convert.ToInt32(cmbloaisp.SelectedValue.ToString());
                    int makho = Convert.ToInt32(cmbmakho.SelectedValue.ToString());
                    int maquay = Convert.ToInt32(cmbmaquay.SelectedValue.ToString());
                    int gia = Convert.ToInt32(txtgia.Text);
                    string tinhtrang = cmbtinhtrang.Text;
                    DateTime ngaynhap = Convert.ToDateTime(dtpngaynhap.Text);

                    SANPHAMBUS spBus = new SANPHAMBUS();
                    SANPHAMDTO spDto = new SANPHAMDTO(ma,tensp, maxx, ghkho, ghquay, slhttrongkho, slhttrongquay, loaisp, makho, maquay, gia, tinhtrang, ngaynhap);
                    spBus.SuaSP(spDto);
                    LoadSP();
                    clr();
                    MessageBox.Show("Thành công");
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void dgvdanhsachsp_SelectionChanged(object sender, EventArgs e)
        {
            lbmasp.Text = dgvdanhsachsp.CurrentRow.Cells[0].Value.ToString();
            txttensp.Text = dgvdanhsachsp.CurrentRow.Cells[1].Value.ToString();
            cmbmaxx.Text = dgvdanhsachsp.CurrentRow.Cells[2].Value.ToString();
            txtgioihankho.Text = dgvdanhsachsp.CurrentRow.Cells[3].Value.ToString();
            txtgioihanquay.Text = dgvdanhsachsp.CurrentRow.Cells[4].Value.ToString();
            txtslhttrongkho.Text = dgvdanhsachsp.CurrentRow.Cells[5].Value.ToString();
            txtslhttrongquay.Text = dgvdanhsachsp.CurrentRow.Cells[6].Value.ToString();
            cmbloaisp.Text = dgvdanhsachsp.CurrentRow.Cells[7].Value.ToString();
            cmbmakho.Text = dgvdanhsachsp.CurrentRow.Cells[8].Value.ToString();
            cmbmaquay.Text = dgvdanhsachsp.CurrentRow.Cells[9].Value.ToString();
            txtgia.Text = dgvdanhsachsp.CurrentRow.Cells[10].Value.ToString();
            cmbtinhtrang.Text = dgvdanhsachsp.CurrentRow.Cells[11].Value.ToString();
            dtpngaynhap.Text = dgvdanhsachsp.CurrentRow.Cells[12].Value.ToString();
        }

        private void txtgioihankho_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtgioihanquay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtgia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtslhttrongkho_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtslhttrongquay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btntim_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                MessageBox.Show("Không có từ khoá cần tìm", "");
                return;
            }
            LoadTim();
        }
    }
}
